﻿namespace Simnova.Collaborate.WPFApplication
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Simnova.Collaborate.Presenter;
    using Simnova.Collaborate.View;
    using System.Runtime.InteropServices;
    using System.Windows.Threading;

    /// <summary>
    /// Interaction logic for TimeLog.xaml
    /// </summary>
    public partial class TimeLog : Window, ITimeLog
    {
        private TimeLogPresenter _timeLogPresenter;
        private DateTime _startTime;
        private int _userId;
        private DispatcherTimer _reminderTimer;
        private DispatcherTimer _timeTimer;

        public TimeLog(int UserID)
        {
            this._userId = UserID;

            this._reminderTimer = new DispatcherTimer();
            this._reminderTimer.Interval = new TimeSpan(0, 15, 0);
            this._reminderTimer.Tick += new EventHandler(this._reminderTimer_Tick);

            this._timeTimer = new DispatcherTimer();
            this._timeTimer.Interval = new TimeSpan(0, 1, 0);
            this._timeTimer.Tick += new EventHandler(this._timeTimer_Tick);
            InitializeComponent();
        }

        #region FlashWindowStuff

        public enum FLASHWINFOFLAGS
        {
            FLASHW_STOP = 0,
            FLASHW_CAPTION = 0x00000001,
            FLASHW_TRAY = 0x00000002,
            FLASHW_ALL = (FLASHW_CAPTION | FLASHW_TRAY),
            FLASHW_TIMER = 0x00000004,
            FLASHW_TIMERNOFG = 0x0000000C
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct FLASHWINFO
        {
            public uint cbSize;
            public IntPtr hwnd;
            public int dwFlags;
            public uint uCount;
            public int dwTimeout;
        }

        [DllImport("user32.dll")]
        public static extern int FlashWindowEx(ref FLASHWINFO pfwi);

        #endregion

        private void _timeTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan elapsedTime = DateTime.Now.Subtract(this._startTime);
            durationTextBox.Text = Convert.ToInt16(elapsedTime.TotalMinutes).ToString();
        }

        private void _reminderTimer_Tick(object sender, EventArgs e)
        {
            // Flash the window
            FLASHWINFO fwi = new FLASHWINFO();
            fwi.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FLASHWINFO)));
            System.Windows.Interop.WindowInteropHelper wih = new System.Windows.Interop.WindowInteropHelper(App.Current.MainWindow);
            fwi.hwnd = wih.Handle;
            fwi.dwFlags = (int)
              (FLASHWINFOFLAGS.FLASHW_ALL | FLASHWINFOFLAGS.FLASHW_TIMERNOFG);
            fwi.dwTimeout = 0;

            FlashWindowEx(ref fwi);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this._timeLogPresenter = new TimeLogPresenter(this, this._userId);

            this._startTime = DateTime.Now;
            durationTextBox.Text = "0";
            this._timeTimer.Start();
        }

        #region ITimeLog Members

        public string Description
        {
            get { return descriptionTextBox.Text;  }
            set { descriptionTextBox.Text = value; }
        }

        DateTime ITimeLog.StartTime
        {
            set { this._startTime = value; }
        }

        public int Duration
        {
            get { return int.Parse(durationTextBox.Text); }
        }

        public System.Collections.IList LogEntries
        {
            set 
            {  
            }
        }

        public System.Collections.IList Companies
        {
            set 
            { 
                companyComboBox.ItemsSource = value;
                companyComboBox.SelectedValuePath = "CompanyId";
                companyComboBox.DisplayMemberPath = "Name";
            }
        }

        public int SelectedCompanyId
        {
            get 
            {
                if (companyComboBox.SelectedValue == null)
                {
                    return 0;
                }

                int companyId;
                int.TryParse(companyComboBox.SelectedValue.ToString(), out companyId);
                return companyId;
            }
        }

        public System.Collections.IList Roles
        {
            set 
            {
                roleComboBox.ItemsSource = value;
                roleComboBox.SelectedValuePath = "RoleId";
                roleComboBox.DisplayMemberPath = "Name";
            }
        }

        public int SelectedRoleId
        {
            get 
            {
                if (roleComboBox.SelectedValue == null)
                {
                    return 0;
                }

                int roleId;
                int.TryParse(roleComboBox.SelectedValue.ToString(), out roleId);
                return roleId;
            }
        }

        public System.Collections.IList ActivityTypes
        {
            set 
            {
                activityTypeComboBox.ItemsSource = value;
                activityTypeComboBox.SelectedValuePath = "ActivityTypeId";
                activityTypeComboBox.DisplayMemberPath = "Name";
            }
        }

        public int SelectedActivityTypeId
        {
            get 
            {
                if (activityTypeComboBox.SelectedValue == null)
                {
                    return 0;
                }

                int activityTypeId;
                int.TryParse(activityTypeComboBox.SelectedValue.ToString(), out activityTypeId);
                return activityTypeId;
            }
        }

        System.Collections.IList ITimeLog.Tasks
        {
            set
            {
                taskComboBox.ItemsSource = value;
                taskComboBox.DisplayMemberPath = "Name";
                taskComboBox.SelectedValuePath = "TaskId";
            }
        }

        int ITimeLog.SelectedTaskId
        {
            get
            {
                if (taskComboBox.SelectedValue == null)
                {
                    return 0;
                }

                int taskId;
                int.TryParse(taskComboBox.SelectedValue.ToString(), out taskId);
                return taskId;
            }
        }

        int ITimeLog.RemainingHours
        {
            get
            {
                int remainingHours = 0;
                int.TryParse(remainingHoursTextBox.Text, out remainingHours);
                return remainingHours;
            }
        }

        #endregion

        private void companyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this._timeLogPresenter == null)
            {
                return;
            }

            this._timeLogPresenter.LoadRoles();
        }

        private void roleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this._timeLogPresenter == null)
            {
                return;
            }

            this._timeLogPresenter.LoadActivityTypes();
        }

        private void activityTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void addEntryButton_Click(object sender, RoutedEventArgs e)
        {
            int tempDuration;
            if (!int.TryParse(durationTextBox.Text, out tempDuration))
            {
                MessageBox.Show("Duration must be Numeric");
            }
            else
            {
                this._timeLogPresenter.SaveTimeLogEntry();
                this._startTime = DateTime.Now;

                durationTextBox.Text = "0";
                descriptionTextBox.Text = "";
                this._timeTimer.Start();
            } 
        }

        private void resetTimerButton_Click(object sender, RoutedEventArgs e)
        {
            if (reminderCheckBox.IsChecked == false)
            {
                this._reminderTimer.Stop();
            }
            else
            {
                this._reminderTimer.Start();
            }
        }
    }
}
